package com.tjsoft.system.web.account;

import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.tjsoft.system.entity.base.Comp;
import com.tjsoft.system.service.AccountManager;
import com.tjsoft.system.web.CrudActionSupport;

//namespace与application下的目录对应dispatcher
@Namespace("/app/sys/comp4select")
public class CompselectAction extends CrudActionSupport<Comp> {

	private static final long serialVersionUID = 8683878162525847072L;

	@Autowired
	private AccountManager accountManager;

	private Page<Comp> page1 = new Page<Comp>(10);
	
	private int page;
	private int limit;
	private String sidx;
	private String sord;
	@Override
	public String delete()  {
		try {
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除微企记录失败");
		}
		return RELOAD;
	}
	@Override
	public String input()  {
		return INPUT;
	}
	
	@Override
	public String list() {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page1 = accountManager.getCompDAO().findPage(page1, filters);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载微企记录失败");
		}
		return "list";
	}
	
	
	@SuppressWarnings("unchecked")
	public void list4test() {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page1 = accountManager.getCompDAO().findPage(page1, filters);
			Struts2Utils.getResponse().setCharacterEncoding("utf-8");
			PrintWriter out = Struts2Utils.getResponse().getWriter();
			JSONObject array = new JSONObject();
			JSONArray rows = new JSONArray();
			
			for(Comp c:page1.getResult()){
				JSONArray obj = new JSONArray();
				JSONObject cell = new JSONObject();
				obj.add(c.getName());
				obj.add( c.getGender());
				obj.add( c.getAge());
				cell.put("cell", obj);
				rows.add(cell);
			}
			array.put("page", page+"");
			array.put("total", "3");
			array.put("records", "2");
			array.put("rows", rows);
			out.write(array.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载微企记录失败");
		}
	}
	
	@Override
	protected void prepareModel()  {
//		if (id != null) {
//			try {
//				entity = accountManager.getCompDAO().get(id);
//			} catch (Exception e) {
//				logger.error(e.getMessage(),e);
//				addActionMessage("加载微企记录失败");
//			}
//		} else {
//			entity = new Comp();
//		}
	}
	@Override
	public String save()  {
		try {
//			accountManager.saveComp(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("保存微企记录失败");
		}
		return RELOAD;
	}
	
	public Comp getModel() {
		return null;
	}
	
	//＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
	public AccountManager getAccountManager() {
		return accountManager;
	}
	
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	
	public Comp getEntity() {
		return null;
	}
	public Page<Comp> getPage1() {
		return page1;
	}
	public void setPage1(Page<Comp> page1) {
		this.page1 = page1;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String getSidx() {
		return sidx;
	}
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}
	public String getSord() {
		return sord;
	}
	public void setSord(String sord) {
		this.sord = sord;
	}
}
